<?php
global $_W,$_GPC;
$settings = $this->module['config'];
$openid = trim($_GPC["openid"]);
$user = pdo_get($this->modulename . "_user", array("openid" => trim($_GPC["openid"]), "uniacid" => $_W["uniacid"]));
if(!$user['unionid']){
	$fans = pdo_get("mc_mapping_fans", array("openid" => $openid));
	$unionid = $fans['unionid'];
}else{
	$unionid = $user['unionid'];
}
if(!$openid){
	exit(json_encode(array('code' => 0, 'status' => 0, 'msg' => '用户信息获取失败')));
}
$invite_uid = intval($_GPC["invite_uid"]);
if(!$invite_uid){$invite_uid = 0;}
if($settings['system_type'] == 1){
	$invite_user = pdo_get($this->modulename . "_user_u", array("user_id" => $invite_uid, "system_id" => $settings['system_id']));
	$invite_user1 = pdo_get($this->modulename . "_user", array("unionid" => $invite_user['unionid'], "uniacid" => $_W["uniacid"]));
}else{
	$invite_user = pdo_get($this->modulename . "_user", array("user_id" => $invite_uid, "uniacid" => $_W["uniacid"]));
	$invite_user1 = $invite_user;
}
if(!$invite_user){
	$parent_id = 0;
	$invite_uid = '0';
	$parent_unionid = 0;
	$invite_unionid = '0';
}else{
	$parent_id = $invite_user['uid'];
	$invite_uid = $invite_user['invite_uid'].'_'.$invite_user['uid'];
	$parent_unionid = $invite_user['unionid'];
	if(empty($invite_user['invite_unionid'])){$invite_user['invite_unionid'] = '0';}
	$invite_unionid = $invite_user['invite_unionid'].'#'.$invite_user['unionid'];
}

if (empty($user)) {
	$data = array();
	if($settings['system_type'] == 1){
		$last_user = pdo_fetchall("SELECT uid FROM ".tablename($this->modulename . "_user")." where uid > 0 and system_id = {$settings['system_id']} order by uid desc limit 1");
	}else{
		$last_user = pdo_fetchall("SELECT uid FROM ".tablename($this->modulename . "_user")." where uniacid = ".$_W["uniacid"]." and uid > 0 order by uid desc limit 1");
	}
	
	if(!$last_user){
		if(intval($settings['uid']) == 0){$settings['uid'] = 16800000;}
		$data['uid'] = $settings['uid'] + 1;
	}else{
		$data['uid'] = $last_user[0]['uid'] + 1;
	}
	$data["uniacid"] = $_W["uniacid"];
	$data["system_id"] = $settings['system_id'];
	$data["openid"] = $openid;
	$data["unionid"] = $unionid;
	$data["parent_id"] = $parent_id;
	$data["invite_uid"] = $invite_uid;
	$data["parent_unionid"] = $parent_unionid;
	$data["invite_unionid"] = $invite_unionid;
	$data["new"] = 1;
	$data["create_time"] = $_W["timestamp"];
	pdo_insert($this->modulename . "_user", $data);
	$user_id = pdo_insertid();
	if($settings['system_type'] == 1 && $unionid){
		$user_u = pdo_get($this->modulename . "_user_u", array("unionid" => $unionid, "system_id" => $settings["system_id"]));
		if(!$user_u){
			$user_uu = pdo_get($this->modulename . "_user_u", array("uid" => $data['uid'], "system_id" => $settings["system_id"]));
			if($user_uu && $user_uu['unionid'] != $unionid){
				$last_user = pdo_fetchall("SELECT uid FROM ".tablename($this->modulename . "_user")." where uid > 0 and system_id = {$settings['system_id']} order by uid desc limit 1");
				if(!$last_user){
					if(intval($settings['uid']) == 0){$settings['uid'] = 16800000;}
					$data['uid'] = $settings['uid'] + 1;
				}else{
					$data['uid'] = $last_user[0]['uid'] + 1;
				}
				pdo_update($this->modulename . "_user", array("uid" => $data['uid']), array("openid" => $openid));
			}
			pdo_insert($this->modulename . "_user_u", $data);
			$user_id = pdo_insertid();
		}
	}
	if($invite_user && $settings['system_type'] == 1){
		$wechats = pdo_get("account_wechats", array("uniacid" => $invite_user["uniacid"]));
		if($wechats){
			$this->newuserNotify($invite_user['openid'],$data['uid']);
		}else{
			$fans_unionid = pdo_get("mc_mapping_fans", array("unionid" => $unionid, "uniacid" => $settings["gzh_id"]));
			if($fans_unionid){
				$this->newuserNotify($fans_unionid['openid'],$data['uid']);
			}
		}
	}
	if($invite_user1 && $invite_user1['s_1_num'] > 0){
		$res = $this->xcxnewuserNotify($invite_user1['openid'],$data['uid']);
	}
	if($invite_user){
		if($settings['system_type'] == 1){
			pdo_update($this->modulename . "_user_u", array('invite_sum +=' => 1), array("user_id" => $invite_user["user_id"]));
		}else{
			pdo_update($this->modulename . "_user", array('invite_sum +=' => 1), array("user_id" => $invite_user["user_id"]));
		}
	}
}else{
	if($_GPC["code"] && $_GPC["iv"] && $_GPC["encryptedData"]){
		$code = $_GPC["code"];
		$appid = $_W["account"]["key"];
		$secret = $_W["account"]["secret"];
		$iv = $_GPC["iv"];
		$encryptedData = $_GPC["encryptedData"];
		$api = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code";
		$res = ihttp_get($api);
		$result = json_decode($res["content"], true);
		$mobile = "";
		$openid = $result["openid"];
		include_once '../addons/'.$this->modulename.'/lib/wxBizDataCrypt.php';
		$wxBizDataCrypt = new WXBizDataCrypt($appid, $result["session_key"]);
		$errCode = $wxBizDataCrypt->decryptData($encryptedData, $iv, $data);
		if($settings['errorcodeid'] == 34){
			var_dump($errCode);
		}
		if ($errCode == 0) {
			$data = json_decode($data, true);
			$mobile = $data["phoneNumber"];
		}
		
	}elseif($_GPC["avatarUrl"] && $_GPC["nickName"]){
		$data['nickName'] = $this->filterEmoji($_GPC["nickName"]);
		if(!$data['nickName']){$data['nickName'] = '用户'.mt_rand(111111,999999);}
		pdo_update($this->modulename . "_user", array("nickname" => $data['nickName'],"avatar" => $_GPC["avatarUrl"]), array("user_id" => $user["user_id"]));
		$user_id = $user['user_id'];
		if($settings['system_type'] == 1){
			pdo_update($this->modulename . "_user_u", array("nickname" => $data['nickName'],"avatar" => $_GPC["avatarUrl"]),  array("unionid" => $user['unionid'], "system_id" => $settings["system_id"]));
		}
	}else{
		if($unionid && !$user['unionid']){
			pdo_update($this->modulename . "_user", array("unionid" => $unionid), array("user_id" => $user["user_id"]));
			$user['unionid'] = $unionid;
		}
		if($settings['system_type'] == 1 && $unionid){
			$user_u = pdo_get($this->modulename . "_user_u", array("unionid" => $unionid, "system_id" => $settings["system_id"]));
			if(!$user_u){
				$user_uu = pdo_get($this->modulename . "_user_u", array("uid" => $user['uid'], "system_id" => $settings["system_id"]));
				if($user_uu && $user_uu['unionid'] != $unionid){
					$last_user = pdo_fetchall("SELECT uid FROM ".tablename($this->modulename . "_user")." where uid > 0 and system_id = {$settings['system_id']} order by uid desc limit 1");
					if(!$last_user){
						if(intval($settings['uid']) == 0){$settings['uid'] = 16800000;}
						$user['uid'] = $settings['uid'] + 1;
					}else{
						$user['uid'] = $last_user[0]['uid'] + 1;
					}
					pdo_update($this->modulename . "_user", array("uid" => $user['uid']), array("openid" => $openid));
				}
				unset($user["user_id"]);
				pdo_insert($this->modulename . "_user_u", $user);
				$user_id = pdo_insertid();
			}
		}
	}
}
if($settings['system_type'] == 1){
	$user = pdo_get($this->modulename . "_user_u", array("unionid" => $unionid, "system_id" => $settings["system_id"]));
	$user_pid = pdo_get($this->modulename . "_pid_u", array("unionid" => $unionid, "system_id" => $settings['system_id']));
}else{
	$user = pdo_get($this->modulename . "_user", array("openid" => $openid, "uniacid" => $_W["uniacid"]));
	$user['follow'] = 0;
	$user_pid = pdo_get($this->modulename . "_pid", array("uid" => $user['user_id'], "uniacid" => $_W["uniacid"]));
}


if($user['is_tuan'] == 0){
	$user['userleveltext'] = $settings['member_name'];
}elseif($user['is_tuan'] == 1){
	$user['userleveltext'] = $settings['tuan_name'];
}elseif($user['is_tuan'] == 2){
	$user['userleveltext'] = $settings['he_name'];
}
if($user['foodlist']){
	$user['foodlist'] = explode(",",$user['foodlist']);
}else{
	$user['foodlist'] = array();
}

$timeToday = mktime("0", "0", "0", date("m"), date("d"), date("y"));
$time = time();
if ($user["sign_in"]) {
	$lastdiff = $user["sign_in"] - $timeToday;
	if ($lastdiff < -86400) {
		$sign_count = 0;
		$sum = 0;
		$user["sign_in_continuity"] = 0;
		$user["sign_in_sum"] = 0;
		$UserInfoData = array("sign_in_continuity" => $sign_count, "sign_in_sum" => $sum);
		if($settings['system_type'] == 1){
			pdo_update($this->modulename . "_user_u", $UserInfoData, array("unionid" => $unionid, "system_id" => $settings['system_id']));
		}else{
			pdo_update($this->modulename . "_user", $UserInfoData, array("openid" => $openid, "uniacid" => $_W["uniacid"]));
		}
	}
}
if($settings['system_type'] == 1){
	$RecordId = pdo_get($this->modulename . "_task_temp", array("unionid" => $unionid, "type" => 3, "system_id" => $settings['system_id']), array("id", "addtime"));
}else{
	$RecordId = pdo_get($this->modulename . "_task_temp", array("openid" => $openid, "type" => 3, "uniacid" => $_W["uniacid"]), array("id", "addtime"));
}

$last_time = date("Y-m-d", $RecordId["addtime"]);
if ($last_time == date("Y-m-d")) {
	$user["User_watch"] = 1;
} else {
	$user["User_watch"] = 2;
}


$backdata = array("openid" => $openid, "userinfo" => $user, "mobile" => $mobile, "pid_time" => $user_pid['use_time']);
exit(json_encode(array('code' => 0, 'status' => 1, 'msg' => 'SUCCESS', 'data' => $backdata)));